<?php

namespace App\Models;

use Carbon\Carbon;
use App\Models\Traits\WithdrawTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;

/**
 * App\Models\Withdraw
 *
 * @property int $id
 * @property int $user_id user
 * @property int $batch_withdraw_id
 * @property string $number number of this withdraw
 * @property string $target_amount how much withdrawing
 * @property string $charge_type deduct service charge from the amount of withdraw money or of user total money
 * @property string $charge_amount pay service charge to platform
 * @property string $actual_amount pay service charge to platform
 * @property string $method withdraw by one of the payments
 * @property string $channel local, freelance
 * @property string|null $remark memo of this withdraw, usually happens with offline
 * @property string|null $info infomation about this withdraw, usually happens with offline
 * @property \Illuminate\Support\Carbon|null $paid_at time of transfer
 * @property string|null $paid_no transaction_id of third party payment
 * @property string $status
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \App\Models\BatchWithdraw $batch
 * @property-read \App\Models\User $user
 * @method static \Database\Factories\WithdrawFactory factory(...$parameters)
 * @method static \Illuminate\Database\Eloquent\Builder|Withdraw newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Withdraw newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Withdraw query()
 * @method static \Illuminate\Database\Eloquent\Builder|Withdraw whereActualAmount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Withdraw whereBatchWithdrawId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Withdraw whereChannel($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Withdraw whereChargeAmount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Withdraw whereChargeType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Withdraw whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Withdraw whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Withdraw whereInfo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Withdraw whereMethod($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Withdraw whereNumber($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Withdraw wherePaidAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Withdraw wherePaidNo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Withdraw whereRemark($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Withdraw whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Withdraw whereTargetAmount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Withdraw whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Withdraw whereUserId($value)
 * @mixin \Eloquent
 * @property string|null $plat_batch_no
 * @method static \Illuminate\Database\Eloquent\Builder|Withdraw wherePlatBatchNo($value)
 * @property string|null $alipay_user_id
 * @method static \Illuminate\Database\Eloquent\Builder|Withdraw whereAlipayUserId($value)
 * @property string $return_free_withdraw_amount
 * @method static Builder|Withdraw todaySandyPayWithdrawAmount()
 * @method static Builder|Withdraw whereReturnFreeWithdrawAmount($value)
 */
class Withdraw extends Model
{
    use HasFactory, WithdrawTrait;
	
	protected $guarded = ['id'];
	
	protected $dates = [
		'paid_at'
	];

    public function scopeTodaySandyPayWithdrawAmount(Builder $builder)
    {
         return $builder->where("method", "sandpay")
                    ->where('status', '!=' , 'failed')
                    ->whereDate('created_at', Carbon::today())
                    ->sum('actual_amount');
    }
   
	
	public function isPaid()
	{
		return (bool)$this->paid_at;
	}
	
	public function user()
	{
		return $this->belongsTo(User::class);
	}
	
	public function batch()
	{
		return $this->belongsTo(BatchWithdraw::class, 'batch_withdraw_id', 'id');
	}
}
